<template>
  <div class="search">
    <div class="search-container">
      <input 
        type="text" 
        v-model="searchQuery" 
        @input="updateSearch"
        placeholder="输入军火名称或类型搜索..."
      >
    </div>

    <div class="search-results" v-if="searchResults.length">
      <div v-for="weapon in searchResults" :key="weapon.id" class="weapon-item">
        <div class="weapon-details">
          <h3>{{ weapon.name }}</h3>
          <p>类型: {{ weapon.type }}</p>
        </div>
        <button 
          :class="['ownership-btn', weapon.owned ? 'owned' : 'not-owned']"
          @click="toggleOwnership(weapon.id)"
        >
          {{ weapon.owned ? '未拥有' : '拥有' }}
        </button>
      </div>
    </div>

    <div class="no-results" v-else-if="searchQuery">
      未找到相关装备
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'

export default {
  name: 'Search',
  setup() {
    const store = useStore()
    const searchQuery = ref('')

    const updateSearch = () => {
      store.commit('setSearchQuery', searchQuery.value)
    }

    const searchResults = computed(() => {
      return store.getters.searchResults
    })

    const toggleOwnership = (weaponId) => {
      store.commit('toggleWeaponOwnership', weaponId)
    }

    return {
      searchQuery,
      searchResults,
      updateSearch,
      toggleOwnership
    }
  }
}
</script>

<style scoped>
.search {
  padding: 80px 2rem 2rem;
  max-width: 800px;
  margin: 0 auto;
}

.search-container {
  margin-bottom: 2rem;
}

.search-container input {
  width: 100%;
  padding: 1rem;
  font-size: 1.1rem;
  border: none;
  border-radius: 8px;
  background-color: #1f1f1f;
  color: white;
}

.search-container input::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

.weapon-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background-color: #1f1f1f;
  border-radius: 8px;
  margin-bottom: 1rem;
}

.weapon-details h3 {
  color: white;
  margin: 0 0 0.5rem;
}

.weapon-details p {
  color: #ff4655;
  margin: 0;
}

.ownership-btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.3s;
}

.ownership-btn.owned {
  background-color: #ff4655;
  color: white;
}

.ownership-btn.not-owned {
  background-color: #4CAF50;
  color: white;
}

.no-results {
  text-align: center;
  color: white;
  padding: 2rem;
  background-color: #1f1f1f;
  border-radius: 8px;
}
</style> 